package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.dao.MessageDao;
import com.example.myapplication.entity.Message;

public class UpdateMessageActivity extends AppCompatActivity {

    // View
    private TextView cancelBtn;
    private TextView saveBtn;
    private EditText usernameEdit;
    private EditText latestMsgEdit;
    private EditText timeEdit;

    private MessageDao messageDao;
    private Message message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_message);
        messageDao = new MessageDao(getBaseContext());
        bindView();
        setMessageToEditText();
    }

    private void setMessageToEditText() {
        Intent intent = this.getIntent();
        message = (Message) intent.getSerializableExtra("messageInfo");
        usernameEdit.setText(message.getUserName());
        latestMsgEdit.setText(message.getLatestMessage());
        timeEdit.setText(message.getTime());
    }

    private void bindView() {
        cancelBtn = findViewById(R.id.tv_cancel_msg);
        saveBtn = findViewById(R.id.tv_save_msg);
        usernameEdit = findViewById(R.id.et_username);
        latestMsgEdit = findViewById(R.id.et_latest_message);
        timeEdit = findViewById(R.id.et_time);

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameEdit.getText().toString();
                String latestMsg = latestMsgEdit.getText().toString();
                String time = timeEdit.getText().toString();
                if (username == null || username.equals("")) {
                    Toast res = Toast.makeText(getBaseContext(), "请输入用户名", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }
                if (latestMsg == null || latestMsg.equals("")) {
                    Toast res = Toast.makeText(getBaseContext(), "请输入消息内容", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }
                if (time == null || time.equals("")) {
                    Toast res = Toast.makeText(getBaseContext(), "请输入时间", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }

                message.setUserName(username);
                message.setLatestMessage(latestMsg);
                message.setTime(time);
                messageDao.update(message);

                Toast res = Toast.makeText(getBaseContext(), "更新成功", Toast.LENGTH_SHORT);
                res.show();
                setResult(1, null);
                finish();
            }
        });

    }
}
